/*eslint-disable*/
// @ts-nocheck

import { Ref } from 'vue';
import {
  BoundingSphere,
  Color,
  defined,
  Ellipsoid,
  SceneTransforms,
  ScreenSpaceEventHandler,
  ScreenSpaceEventType,
  Viewer,
} from '@space8/czm-plugin';
import { getFieldInfo } from '@/api/mapServices/mapData';
import { FieldInfo } from '@/components/GlobalCesium/src/type';

export function useFieldPopup(
  show: Ref<boolean>,
  left: Ref<number>,
  top: Ref<number>,
  popupData: FieldInfo,
  viewer: Viewer,
) {
  let position = null;
  let selectedEntity = null;

  function registerFieldClick() {
    const screenSpaceEventHandler = new ScreenSpaceEventHandler(viewer.canvas);
    screenSpaceEventHandler.setInputAction((clickEvent: any) => {
      const pickObject = viewer.scene.pick(clickEvent.position);
      if (
        defined(pickObject) &&
        defined(pickObject.id) &&
        defined(pickObject.id.type) &&
        pickObject.id.type === 'tiandi' &&
        defined(pickObject.id.polygon)
      ) {
        // 确保拾取的是田地
        const name = pickObject.id.name;

        // 设置选中
        if (selectedEntity) {
          selectedEntity.material = Color.fromCssColorString('#ABFF8D').withAlpha(0.5); // 去掉之前entity的选中状态
          selectedEntity = null;
        }
        selectedEntity = pickObject.id.polygon;
        selectedEntity.material = Color.fromCssColorString('#ABFF8D'); // 设置选中状态

        // 计算中心点
        const points = selectedEntity.hierarchy.getValue().positions;
        let center = BoundingSphere.fromPoints(points).center;
        center = Ellipsoid.WGS84.scaleToGeodeticSurface(center);
        viewer.scene.clampToHeightMostDetailed([center]).then((res) => {
          position = res[0] === undefined ? center : res[0];
        });

        getFieldInfo(name).then((res) => {
          popupData.title = name;
          if (res.baseinfoFarmlandDetailVo) {
            popupData.field1 = res.baseinfoFarmlandDetailVo.dahuName;
            popupData.field3 = Number(res.baseinfoFarmlandDetailVo.area / 666).toFixed(2);
            popupData.field4 = '';
            popupData.field5 = '';
            popupData.field6 = '';
            popupData.field7 = '';
            popupData.field8 = '';
            popupData.field9 = '';
          }
          if (res.waterCropsPlantDetailVoList) {
            popupData.field2 = res.baseinfoFarmlandDetailVo.type;
            popupData.field10 = res.waterCropsPlantDetailVoList[0].plantInfoList[0].startTime;
            popupData.field11 = res.waterCropsPlantDetailVoList[0].plantInfoList[0].photoList;
            popupData.field12 = res.waterCropsPlantDetailVoList[0].plantInfoList[0].status;
          } else {
            popupData.field2 = res.baseinfoFarmlandDetailVo.type;
            popupData.field10 = '';
            popupData.field11 = '';
            popupData.field12 = '';
          }
          show.value = true;
        });
      } else {
        closeFieldWindow();
      }
    }, ScreenSpaceEventType.LEFT_CLICK);

    viewer.scene.preRender.addEventListener(() => {
      if (position && show.value) {
        const coordinates = SceneTransforms.worldToWindowCoordinates(viewer.scene, position);
        if (coordinates) {
          left.value = coordinates.x;
          top.value = coordinates.y;
        }
      }
    });
  }

  function closeFieldWindow() {
    show.value = false;
    position = null;
    if (selectedEntity) {
      selectedEntity.material = Color.fromCssColorString('#ABFF8D').withAlpha(0.5);
      selectedEntity = null;
    }
  }

  return {
    registerFieldClick,
    closeFieldWindow,
  };
}
